package chart;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

// 堆叠条形图
public class StackedBarChartDemo extends Application {
	private Pane root = new Pane();
	private CategoryAxis x = new CategoryAxis();
	private NumberAxis y = new NumberAxis(0, 100, 10);
	private StackedBarChart<String, Number> sbc = new StackedBarChart<>(x, y);
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		
		sbc.setPrefSize(400, 400);
		x.setLabel("属性");
		y.setLabel("数量");
		
		XYChart.Data<String, Number> data1 = new XYChart.Data<String, Number>("A", 30);		// 数据(自动合并)
		XYChart.Data<String, Number> data2 = new XYChart.Data<String, Number>("A", 40);
		XYChart.Data<String, Number> data3 = new XYChart.Data<String, Number>("B", 60);
		XYChart.Data<String, Number> data4 = new XYChart.Data<String, Number>("B", 20);
		
		XYChart.Series<String, Number> xy1 = new XYChart.Series<>();	// 每种属性中有几个矩形
		xy1.setName("A");
		xy1.getData().add(data1);
		xy1.getData().add(data3);
		
		XYChart.Series<String, Number> xy2 = new XYChart.Series<>();
		xy2.setName("B");
		xy2.getData().add(data2);
		xy2.getData().add(data4);
		
		sbc.getData().add(xy1);
		sbc.getData().add(xy2);
		root.getChildren().add(sbc);
		
		Scene scene = new Scene(root, 400, 400);
		stage.setScene(scene);
		stage.show();
	}
}
